﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BE_CruzDelSur;
using System.Data.SqlClient;
using System.Data;
using BR_CruzDelSur.Interfaces;

namespace BR_CruzDelSur
{
    public class BR_Pago : IBR_Pago
    {
        Conexion conn = new Conexion();
        IBR_Util EEstado = new BR_Util();

        public BE_Pago f_obtenerPagoXCarga(string codigo)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("INT_CODIGO_CARGA", SqlDbType.Int);
            param[0].Value = codigo;
            param[0].Direction = ParameterDirection.Input;

            DataSet ds = SqlHelper.ExecuteDataSet(conn.CadenaConexion, System.Data.CommandType.StoredProcedure, "SP_OBTENERPAGOXCARGA", param);
            BE_Pago pago = null;

            foreach (DataRow item in ds.Tables[0].Rows)
            {
                pago = new BE_Pago()
                {
                    CodigoCarga = Convert.ToInt32(item["INT_CODIGO_CARGA"].ToString()),
                    Monto = Convert.ToDouble(item["DBL_MONTO"].ToString()),
                    Estado = item["CHR_ESTADO"].ToString(),
                    FechaRegistro = Convert.ToDateTime(item["DTM_FECHA_REGISTRO"].ToString())
                };

                return pago;
            }
            return pago;
        }

        public bool f_EliminarPago(int carga)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("INT_CODIGO_CARGA", SqlDbType.Int);
            param[0].Value = carga;
            param[0].Direction = ParameterDirection.Input;

            try
            {
                SqlHelper.ExecuteNonQuery(conn.CadenaConexion, System.Data.CommandType.StoredProcedure, "SP_ELIMINARPAGO", param);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool f_InsertarPago(double monto, string estado, string carga)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("DBL_MONTO", SqlDbType.VarChar);
            param[0].Value = monto;
            param[0].Direction = ParameterDirection.Input;
            param[1] = new SqlParameter("CHR_ESTADO", SqlDbType.VarChar);
            param[1].Value = estado;
            param[1].Direction = ParameterDirection.Input;
            param[2] = new SqlParameter("INT_CODIGO_CARGA", SqlDbType.Int);
            param[2].Value = carga;
            param[2].Direction = ParameterDirection.Input;

            try
            {
                SqlHelper.ExecuteNonQuery(conn.CadenaConexion, System.Data.CommandType.StoredProcedure, "SP_INSERTARPAGO", param);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool f_ActualizarPago(string estado, string carga)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("CHR_ESTADO", SqlDbType.VarChar);
            param[0].Value = estado;
            param[0].Direction = ParameterDirection.Input;
            param[1] = new SqlParameter("INT_CODIGO_CARGA", SqlDbType.Int);
            param[1].Value = carga;
            param[1].Direction = ParameterDirection.Input;

            try
            {
                SqlHelper.ExecuteNonQuery(conn.CadenaConexion, System.Data.CommandType.StoredProcedure, "SP_ACTUALIZARPAGO", param);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
